package com.poposchool.project.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.poposchool.project.common.Result;
import com.poposchool.project.entity.StoreProduct;
import org.apache.commons.math3.analysis.function.Min;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;


import com.poposchool.project.service.IStoreOrderService;
import com.poposchool.project.entity.StoreOrder;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author wangjie
 * @since 2022-09-09
 */
@RestController
@RequestMapping("/storeOrder")
public class StoreOrderController {

    @Resource
    private IStoreOrderService storeOrderService;

    // 新增或者更新
    @PostMapping
    public boolean save(@RequestBody StoreOrder storeOrder) {
        return storeOrderService.saveOrUpdate(storeOrder);
    }

    @GetMapping("/allCount")
    public Result getAllNum(){
        return Result.success(storeOrderService.count());
    }

    @GetMapping("/allPay")
    public Result allPay(){
        return Result.success(storeOrderService.getAllPrice());
    }

    @GetMapping("/allCost")
    public Result allCost(){
        return Result.success(storeOrderService.getAllCost());
    }

    @GetMapping("/allPayByEveryMonth")
    public Result allPayByEveryMonth(){
        return Result.success(storeOrderService.getAllPriceByMonth());
    }

    @DeleteMapping("/{id}")
    public Boolean delete(@PathVariable Integer id) {
        return storeOrderService.removeById(id);
    }

    @PostMapping("/del/batch")
    public boolean deleteBatch(@RequestBody Map<String, List<Integer>> ids) {
        return storeOrderService.removeByIds(ids.get("ids"));
    }

    @GetMapping
    public List<StoreOrder> findAll() {
        return storeOrderService.list();
    }

    @GetMapping("/{id}")
    public StoreOrder findOne(@PathVariable Integer id) {
        return storeOrderService.getById(id);
    }

//    @GetMapping("/page")
//    public Page<StoreOrder> findPage(@RequestParam Integer pageNum,
//                                     @RequestParam Integer pageSize) {
//        QueryWrapper<StoreOrder> queryWrapper = new QueryWrapper<>();
//        queryWrapper.orderByDesc("id");
//        return storeOrderService.page(new Page<>(pageNum, pageSize), queryWrapper);
//    }

    /**
     * 查询流水
     *
     * @param id
     * @return
     */
    @GetMapping("/page")
    public Page<StoreOrder> findRunningAmountBy(
            @RequestParam Integer pageNum,
            @RequestParam Integer pageSize,
            @RequestParam(value = "id", required = false) Integer id,
            @RequestParam(value = "uid", required = false) Integer uid,
            @RequestParam(value = "realName", defaultValue = "", required = false) String realName,
            @RequestParam(value = "startTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            @RequestParam(value = "endTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime,
            @RequestParam(value = "MinPayPrice", required = false) BigDecimal MinPayPrice,
            @RequestParam(value = "MaxPayPrice", required = false) BigDecimal MaxPayPrice) {
        QueryWrapper<StoreOrder> queryWrapper = new QueryWrapper<>();
        if (realName != "" || realName != null){
            queryWrapper.like("real_name",realName);
        }
        if (id != null){
            queryWrapper.eq("id",id);
        }
        if (uid != null){
            queryWrapper.eq("uid",uid);
        }
        if (startTime != null && endTime != null){
            queryWrapper.between("create_time",startTime,endTime);
        }
        if (MaxPayPrice == null && MinPayPrice == null){
            queryWrapper.orderByDesc("id");
            return storeOrderService.page(new Page<>(pageNum, pageSize), queryWrapper);
        }
        if (MinPayPrice == null){
            MinPayPrice = new BigDecimal(0);
        }
        if (MaxPayPrice == null){
            MaxPayPrice = new BigDecimal(0);
        }
        queryWrapper.between("pay_price",MinPayPrice,MaxPayPrice);
        queryWrapper.orderByDesc("id");
        return storeOrderService.page(new Page<>(pageNum, pageSize), queryWrapper);
    }

}

